package com.tinem.platform.maven.archetype.i18n;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.setting.dialect.Props;
import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className ExcelMojo
 * @description TODO
 * @site 
 * @company 
 * @create 2020-06-28 15:32
 */
@Mojo( name = "excel")
@Data
@EqualsAndHashCode(callSuper=false)
public class ExcelMojo extends AbstractMojo {
    @Parameter( defaultValue = "${project.resources}", property = "resources", required = true )
    private List<Resource> resources;

    @Parameter( defaultValue = "${project}", property = "object", required = true )
    private Object object;
    @Parameter( defaultValue = "${session}", property = "session", required = true )
    private Object session;

    @Parameter( defaultValue = "${project.build.outputDirectory}", property = "outputDirectory", required = true )
    private File outputDirectory;

    @Parameter(name ="basename" , defaultValue = "i18n/messages")
    private String basename;
    @Parameter(name ="messageConfig" , defaultValue = "messages.xlsx")
    private String messageConfig;
    @Parameter(name ="messageDir" )
    private String messageDir;

    private String messageName;
    private File messageResourcesDir;
    private File messageTargetDir;

    @Override
    public void execute() throws MojoExecutionException
    {
        System.out.println("-------------------------------------------------------------------------------------------------------------------------");
//        System.out.println(JSON.toJSONString(System.getProperties(),true));
        System.out.println("-------------------------------------------------------------------------------------------------------------------------");
//        System.out.println(JSON.toJSONString(System.getenv(),true));
        System.out.println("-------------------------------------------------------------------------------------------------------------------------");
//        System.out.println(JSON.toJSONString(session,true));
        System.out.println("-------------------------------------------------------------------------------------------------------------------------");
        File file = new File(outputDirectory,basename);
        messageName = file.getName();
        messageTargetDir = file.getParentFile();
        File messageConfigFile=null;
        if(messageDir == null){
            for (Resource resource : resources) {
                messageConfigFile = new File(resource.getDirectory(), messageConfig);
                messageResourcesDir = new File(resource.getDirectory(), basename).getParentFile();
                if(messageConfigFile.exists()) {
                    break;
                }
                messageConfigFile = new File(new File(resource.getDirectory(),basename).getParentFile(), messageConfig);
                if(messageConfigFile.exists()) {
                    break;
                }
            }
        }else{
            messageConfigFile = new File(messageDir,messageConfig);
        }

        if(!messageConfigFile.exists()) {
            throw new MojoExecutionException("messageConfig: "+messageConfigFile.getAbsolutePath()+", 未找到");
        }

        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(messageConfigFile),messageName);
        List<List<Object>> readAll = reader.read();
        Map<String, Props> propsMap=new HashMap<>();
        for (int i = 2; i < readAll.size(); i++) {
            List<Object> line = readAll.get(i);
            String key = line.get(0)+"";
            for (int r = 1; r < line.size(); r++) {
                String locale = readAll.get(1).get(r)+"";
                Props props = propsMap.get(locale);
                if(props == null) {
                    props = new Props();
                    propsMap.put(locale, props);
                }
                props.setProperty(key, line.get(r)+"");
            }
        }
        propsMap.get(readAll.get(1).get(1)).store(new File(messageTargetDir,messageName+".properties").getAbsolutePath());
        for (String key : propsMap.keySet()) {
            if(StrUtil.isBlank(key)) {
                propsMap.get(key).store(new File(messageTargetDir,messageName+".properties").getAbsolutePath());
            }else {
                propsMap.get(key).store(new File(messageTargetDir,messageName+"_"+key+".properties").getAbsolutePath());
            }
        }
    }
}
